
/**	
*	
	Ford circles : another try in circle/spher packing
*/

import ij.ImagePlus;
import ij.plugin.PlugIn;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


import java.awt.Color;
import java.awt.Font;
import java.awt.Polygon;
import java.io.PrintStream;


import java.util.ArrayList;
import java.util.Locale;
import java.util.Random; 




import pluginUtils.Square;
import pluginUtils.Complexe;



public class City extends ImagePlus implements PlugIn{
	{Locale.setDefault(Locale.US); }
	
	private ImageProcessor primeIP; 
	private int X=2000,Y=2000; // image size
	
	private Random generator=new Random(); // One always needs some randomness
	
	// For the scaling of the image
	private double x0=0,x1=1,y0=0,y1=1;
	private double acoef,ccoef,vcoef,wcoef; 
	
	
	
	public void setCoefs(){
		acoef=X/(x1-x0); 
		vcoef=Y/(y1-y0); 
		ccoef=-acoef*x0; 
		wcoef=-vcoef*y0; 
	}
	 
	private PrintStream output;
	
	public void setBounds(double xl,double xu,double yl,double yu){
		x0=xl; 
		x1=xu;
		y0=yl;
		y1=yu; 
	}
	
	
	private boolean premier(int p,int q){
		while(p!=q){
			if(p>q) p=p-q; 
			else q=q-p; 
		}
		return (q==1);
	}
	
	private Polygon dessineCarre(Square e){
		double x=e.getX(); 
		double y=e.getY(); 
		double L=e.getLongueur(); 
		double l=e.getLargeur(); 
		double h=e.getH(); 
		int xi=(int)(x*X); 
		int yi=(int)(y*Y); 
		int xs=(int)((x+L)*X); 
		int ys=(int)((y+l)*Y); 
		
		Polygon carre=new Polygon(new int[]{xi,xi,xs,xs},new int[]{yi,ys,ys,yi},4);
		return carre; 
	}
	
	
	
	
	
	private void drawLine(double xd,double yd,double xf,double yf){
		int XD=(int)(xd*acoef+ccoef); 
		int YD=(int)(yd*vcoef+wcoef); 
		int XF=(int)(xf*acoef+ccoef); 
		int YF=(int)(yf*vcoef+wcoef);
		primeIP.drawLine(XD,YD,XF,YF); 
	}
	
	

	
	
	
	
	public void run(String arg){
		
		
		primeIP = new ColorProcessor(X,Y);
		
		setBounds(-1,1,-1,1); 
		setCoefs(); 
		
		
		
		
		
		
		Polygon background=new Polygon(new int[]{0,0,X,Y},new int[]{0,X,Y,0},4);
		primeIP.setColor(Color.WHITE);
		
		primeIP.fillPolygon(background);
		
		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.BLACK);
		primeIP.setLineWidth(5); 
		
		//drawLine(0,y0,0,y1); 
		//drawLine(x0,0,x1,0);
		
		//primeIP.moveTo(150,80); 
		//primeIP.drawString("a="+a+"\n"+"b="+b+"\n"+"n="+n+"\n"+"m="+m);
	
		setProcessor("Verifying", primeIP);
		/************************ The drawing begins here *******************************/
		ArrayList<Square>city=new ArrayList<Square>(); 
		
		city.add(new Square(x0,y0,x1-x0,y1-y0,1)); 
		int nbSquares=0; 
		try{
			output=new PrintStream("/tmp/outCity.txt"); 
			for(int q=0; q<5;q++)
			{
				ArrayList<Square>city2=new ArrayList<Square>();
				for(Square e:city){
					double x=e.getX(); 
					double y=e.getY(); 
					double L=e.getLongueur(); 
					double l=e.getLargeur(); 
					double h=e.getH(); 
					double a=(0.3+generator.nextDouble()/5)*L;
					double b=(0.3+generator.nextDouble()/5)*l; 
					city2.add(new Square(x,y,L-a,l-b,h*(1-generator.nextDouble()/2))); 
					city2.add(new Square(x+L-a,y,a,l-b,h*(1-generator.nextDouble()/2))); 
					city2.add(new Square(x,y+l-b,L-a,b,h*(1-generator.nextDouble()/2))); 
					city2.add(new Square(x+L-a,y+l-b,a,b,h*(1-generator.nextDouble()/2))); 
				}
				city=city2; 
				
			}
		}
			catch(Exception e){System.out.println("dans le baba"); System.exit(0); }
		for(Square ex:city){
			
			
			String s="box{<"+ex.getX()+",0,"+ex.getY()+">,<"+(ex.getX()+ex.getLongueur())+","+ex.getH()+","+(ex.getY()+ex.getLargeur())+">";
			s+= " pigment{color rgbt<"+generator.nextDouble()+","+generator.nextDouble()+","+(0.9+generator.nextDouble()/10)+","+(0.5+generator.nextDouble()/4)+">}"; 
			s+="finish{Finnish0} interior{ior "+(1+generator.nextDouble()/5)+"}}" ;
			
			
			System.out.println(s); 
			output.println(s);
			
			primeIP.drawPolygon(dessineCarre(ex)); 
		}
		
		
		
		show();
		System.out.println("**********************************************done "+city.size()); 
	
		
	}

}
	
	